function Snake(el){
        this.el = el
        this.len = 20
    this.snakeObj = [
        { x: 3, y :2, color:'red', el:null},  //蛇头
        { x: 2, y :2, color:'blue', el:null}, // 蛇的身体
        { x: 1, y :2, color:'blue', el:null}  // 蛇的身体
      ]
      this.direction = 'right'
}
// 创造蛇
Snake.prototype.create = function(){
         this.remove()
          for(let i = 0; i<this.snakeObj.length;i++ ){
            let divobj = document.createElement('div')
            divobj.style.width = this.len + 'px'
            divobj.style.height = this.len + 'px'
            divobj.style.background = this.snakeObj[i].color
            divobj.style.left = this.snakeObj[i].x * this.len
            divobj.style.top = this.snakeObj[i].y  * this.len
            divobj.style.position = 'absolute'
            // 放到地图上
            this.el.appendChild(divobj)
            this.snakeObj[i].el = divobj


          }   

}
Snake.prototype.move=function(){
    for(let i =  this.snakeObj.length-1; i>0 ; i-- ){
        this.snakeObj[i].x = this.snakeObj[i-1].x 
        this.snakeObj[i].y =  this.snakeObj[i-1].y 
    }
    switch(this.direction){
        case 'top':
        this.snakeObj[0].y-=1
        break;
        case 'bottom':
        this.snakeObj[0].y+=1
        break;
        case 'left':
        this.snakeObj[0].x-=1
        break;
        case 'right':
        this.snakeObj[0].x+=1
        break;

    }
    this.create()
}

Snake.prototype.remove=function(){
    for(let i = 0 ; i<this.snakeObj.length ; i++ ){
 
    if(this.snakeObj[i].el){
        this.el.removeChild(this.snakeObj[i].el)
}
 
}
}